package web.controller.address_telefon;

import java.io.Serializable;

import hibernate.pojo.User;
import hibernate.util.HibernateUtil;

import javax.faces.context.FacesContext;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

public class ChangePasswordController {

	private static final Log logger = LogFactory.getLog(ChangePasswordController.class);
	
	private String oldPassword;
	private String newPassword;
	private String newPasswordConfirm;
	private User user;

	public ChangePasswordController() {
		// TODO Auto-generated constructor stub
	}
	
	public String changePassword(){

		String pageToRedirect="";
		String messageToDisplay="";
		
		System.out.println(oldPassword);
		System.out.println(newPassword);
		System.out.println(newPasswordConfirm);
		
		logger.info(oldPassword);
		logger.info(newPassword);
		logger.info(newPasswordConfirm);
		
		if(confirmPassword()){
			try{
				FacesContext context = FacesContext.getCurrentInstance();
				User user = (User)context.getExternalContext().getSessionMap().get("user");

				user.setPassword(newPassword);
				Session session = HibernateUtil.getSessionFactory().openSession();
				Transaction transaction = session.beginTransaction();
				session.update(user);
				transaction.commit();
				return "changePasswordSuccess";
			}catch(Exception ex){
				//System.out.println("Alınan hata mesaji : "+ex.toString());
				logger.error("Alınan hata mesaji : "+ex.toString());
			}

		}
		return "changePasswordSuccess";
		//return pageToRedirect;
	}

	private boolean confirmPassword(){
		if(!newPassword.equals(newPasswordConfirm)){
			return false;
		}
		return true;
	}

	public String backToMain(){
		
		return "backToMain";
		
	}
	
	public String getOldPassword() {
		return oldPassword;
	}
	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	public String getNewPassword() {
		return newPassword;
	}
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}
	public String getNewPasswordConfirm() {
		return newPasswordConfirm;
	}
	public void setNewPasswordConfirm(String newPasswordConfirm) {
		this.newPasswordConfirm = newPasswordConfirm;
	}




}
